//
//  GRXSettingController.m
//  1-MushroomStreet-自定义tabbar
//
//  Created by xmg on 15/12/19.
//  Copyright © 2015年 grx. All rights reserved.
//

#import "GRXSettingController.h"
#import "GRXSettingItem.h"
#import "GRXSettingCell.h"
#import "NSObject+FileManager.h"
//#define __weakSelf   __weak typeof(self) weakSelf = self//定义了一个弱指针

@interface GRXSettingController ()

/** 缓存尺寸*/
@property(nonatomic ,assign) NSInteger total;

@end

@implementation GRXSettingController

#pragma mark ---系统回调方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化tableView
    [self setUpTableView];
    
    //设置底部footerView
    [self setFooterView];
    

}

//计算计算缓存内容尺寸
- (void)calculateCacheSize{
    [self getFileCacheSizeWithPath:self.cachePath completion:^(NSInteger total) {
        _total = total;
        
        //设置cell显示内容
        [self setCellContent];
        
        // 计算完成就会调用
        [self.tableView reloadData];
        
    }];
}

#pragma mark ---初始化cell
- (void)setUpTableView{
    
    self.title = @"设置";
    
    //设置tableView背景颜色
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
    
    //调整组与组之间的距离(头部视图高度和尾部视图高度组成)
    self.tableView.sectionHeaderHeight = 10;//调整头部视图高度(有默认高度)
    self.tableView.sectionFooterHeight = 0;//调整尾部视图高度(有默认高度)
    
    //设置tableView的滚动区域
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 100, 0);
    self.tableView.bounces = NO;
}

- (void)viewWillAppear:(BOOL)animated{
    //计算缓存内容尺寸
    [self calculateCacheSize];
    
    [super viewWillAppear:animated];
    


}

#pragma mark ---cell显示内容
- (void)setCellContent{
    //    __weakSelf;//声明了一个弱指针
    GRXSettingItem *info = [GRXSettingItem modelWithTitle:@"个人信息" type:GRXSettingCellTypeArrow];
    GRXSettingItem *psw = [GRXSettingItem modelWithTitle:@"登录密码" type:GRXSettingCellTypeArrow];
    GRXSettingItem *publish = [GRXSettingItem modelWithTitle:@"发表同步到本地相册" type:GRXSettingCellTypeSwitch];
    GRXSettingItem *open = [GRXSettingItem modelWithTitle:@"开启悬浮窗" type:GRXSettingCellTypeSwitch];
    GRXSettingItem *animation = [GRXSettingItem modelWithTitle:@"开启动画提示" type:GRXSettingCellTypeSwitch];
    
    
    GRXSettingItem *share = [GRXSettingItem modelWithTitle:@"开启截屏分享功能" type:GRXSettingCellTypeSwitch];
    GRXSettingItem *clear = [GRXSettingItem modelWithTitle:@"清除缓存" type:GRXSettingCellTypeLabel];
    clear.detail = [self getCachesSize];
    clear.action = ^(){
        //清除缓存
        [self cleaCaches];
    };
    

    GRXSettingItem *help = [GRXSettingItem modelWithTitle:@"帮助反馈" type:GRXSettingCellTypeArrow];
    GRXSettingItem *about = [GRXSettingItem modelWithTitle:@"关于蘑菇街" type:GRXSettingCellTypeArrow];
    
    
    _dataArray = @[
                   @[info,psw,publish,open,animation],
                   @[share,clear],
                   @[help,about]
                   ];
}

#pragma mark ---数据缓存处理
//清除缓存数据
- (void)cleaCaches{
    // 清空缓存,就是把Cache文件夹直接删掉
    // 删除比较耗时
    [self removeCacheWithCompletion:^{
        
        _total = 0;
        
        [self.tableView reloadData];
    }];
}

//显示数据缓存尺寸
- (NSString *)getCachesSize{
    NSString *cacheStr = @"0.0M";
    NSLog(@"--%.1ld",(long)_total);
    if (_total) {
        CGFloat totalF = _total;
        NSString *unit = @"B";
        if (_total > 1000 * 1000) { // MB
            totalF = _total / 1000.0 / 1000.0;
            unit = @"MB";
        }else if (_total > 1000){ // KB
            unit = @"KB";
            totalF = _total / 1000.0 ;
        }
        
        cacheStr = [NSString stringWithFormat:@"已使用%.1f%@",totalF,unit];
    }
    
    return cacheStr;
}

#pragma mark ---设置footerView
- (void)setFooterView{
    UIView *footer = [[UIView alloc] init];
    footer.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = footer;
    footer.width = self.view.width;
    footer.height = 100;
    
    //添加按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"退出当前账号" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.width = 200;
    btn.height = 40;
    btn.center = footer.center;
    [btn setBackgroundColor:kGRXColor(255, 70, 112)];
    [footer addSubview:btn];
    
}



#pragma mark ---数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //取出每一组的数据,保存在一个小数组中
    NSArray *array = _dataArray[section];
    
    //返回每一个小组的元素个数,也就是每一组的行数
    return array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    GRXSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[GRXSettingCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    //取出每一组每一行的模型数据,然后给创建的cell赋值
    GRXSettingItem *item = _dataArray[indexPath.section][indexPath.row];
    
    cell.item = item;
    
    return cell;
}

#pragma mark ---代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //创建模型(通过模型对象,拿到选中的cell)
    GRXSettingItem *item = _dataArray[indexPath.section][indexPath.row];
    
    //判断block是否为空,如果不为空就执行block后面的代码
    if (item.action) {
        item.action();
    }
}


@end
